package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.exception.MyException;
import com.example.pojo.Manage;
import com.example.pojo.Query;
import com.example.service.ManageService;
import com.example.utils.FileUploadUtil;
import com.example.utils.Page;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
@RequestMapping("/manage")
@Api(tags = "用户管理")
public class ManageController {
    @Autowired
    private ManageService manageService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private FileUploadUtil fileUploadUtil;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody Manage manage){
        Manage manage1= manageService.login(manage);
        Map<String,Object> map=new HashMap<>();
        String token="";
        if (manage1!=null){
            if (manage.getPassword().equals(manage1.getPassword())){
                map.put("data",manage1);
                map.put("code",222);
                map.put("msg","登录成功");
                //产生令牌 保存到redis 给前端
                token = UUID.randomUUID().toString();
                String json= JSONObject.toJSONString(manage1); //将user对象转换为json字符串
                stringRedisTemplate.opsForValue().set(token,json,30, TimeUnit.MINUTES); //将token(键)和user对象(值)存入redis
                map.put("token",token);
            }else {
                map.put("code",444);
                map.put("msg","密码错误");
            }
        }else {
            map.put("code",333);
            map.put("msg","用户不存在");
        }
        return new Result<>("200", "登录成功",map);
    }

    @GetMapping("/selectAllManage")
    public Result selectAllManage(Query query){//分页查询所有管理员信息
        Page<Manage> page=new Page<>(query.getPageNum(),query.getPageSize(),manageService.selectManageCount(query.getManager()));
        page.setList(manageService.selectAllManage(query.getManager(),page.getStart(),query.getPageSize()));
        Map<String,Object> map=new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());//总条数
        map.put("pageSize", query.getPageSize());//每页显示几条
        map.put("pageNum", query.getPageNum());//当前页码
        return new Result<>("200", "查询成功", map);
    }

    @GetMapping("/selectManageById")
    public Result selectManageById(Integer id){//根据id查询管理员信息
        Manage manage=manageService.selectManageById(id);
        Map<String,Object> map=new HashMap<>();
        if (manage!=null){
            map.put("code",200);
            map.put("data",manage);
            map.put("msg","查询成功");
        }else {
            map.put("code",500);
            map.put("data",null);
            map.put("msg","查询失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @GetMapping("/deleteManage")
    public Result deleteManage(Integer id) {//删除管理员信息
        int i=manageService.deleteManage(id);
        Map<String,Object> map=new HashMap<>();
        if (i>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else {
            map.put("code",444);
            map.put("msg","删除失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @PostMapping("/addManage")
    public Result addManage(@RequestBody Manage manage) {//添加管理员信息
        int i = manageService.addManage(manage);
        Map<String, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", 200);
            map.put("msg", "添加成功");
        } else {
            map.put("code", 444);
            map.put("msg", "添加失败");
        }
        return new Result<>("222", "调用成功", map);
    }
    @PostMapping("/updateManage")
    public Result updateManage(@RequestBody Manage manage) {//修改管理员信息
        int i = manageService.updateManage(manage);
        Map<String, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", 200);
            map.put("msg", "修改成功");
        }else {
            map.put("code", 444);
            map.put("msg", "修改失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    //上传图片
    @PostMapping("/upload-img")
    public Result uploadImg(@RequestParam("file") MultipartFile file){
        InputStream inputStream = null;
        try{
            inputStream = file.getInputStream();
        }catch (IOException e){
            throw new MyException("606","上传时发生异常");
        }
        String url = fileUploadUtil.upload(inputStream,file.getOriginalFilename());
        return new Result("200","上传成功",url);
    }

    //删除图片
    @RequestMapping("/delete-img")
    public Result deleteImg(@RequestParam("url") String url){
        fileUploadUtil.removeFile(url);
        return new Result("200","删除成功",null);
    }

}
